//
//  NSMapTable+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/9/23.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//
#import "NSMapTable+RAC.h"
#import "NSMapTable+RACSequenceAdditions.h"

@implementation NSMapTable (RAC)

- (RACSignal *)rac_traverseMapTable
{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [self rac_traverseMapTableForSubscribeNext:^(id key, id value) {
            
            [subscriber sendNext:@[
                                   key,
                                   value,
                                   ]];
        } traverseCompleted:^{
            
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

- (void)rac_traverseMapTableForSubscribeNext:(void(^)(id key, id value))next traverseCompleted:(void(^)())completed
{
    [[self.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(RACTuple *tuple) {
        
        RACTupleUnpack(id key, id value) = tuple;
        if (next) {
            next(key, value);
        }
        
    } completed:^{
        
        if (completed) {
            completed();
        }
        
    }];
}

@end
